package com.example.myapplication.util;

import android.util.Log;
import com.example.myapplication.bean.Inspection;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import okhttp3.*;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class HttpUtils {
    private static final String TAG = "HttpUtils";
    public static String get(String url, Map<String, String> params) {
        StringBuilder stringBuilder = new StringBuilder(url);
        if (params != null && params.size() != 0) {
            stringBuilder.append("?");
            for (Map.Entry<String, String> param : params.entrySet()) {
                stringBuilder.append(param.getKey()).append("=").append(param.getValue()).append("&");
            }
        }
        FutureTask<String> responseTask = new FutureTask<>(() -> {
            String responseJsonData = null;
            try {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .get()
                        .url(stringBuilder.toString())
                        .build();
                Call call = client.newCall(request);
                Response response = call.execute();
                responseJsonData = response.body().string();
            } catch (Exception e) {
                Log.e(TAG, "get: " + e);
            }
            return responseJsonData;
        });

        Thread responseThread = new Thread(responseTask);
        responseThread.start();
        String responseJsonData = null;
        try {
            responseJsonData = responseTask.get();
        } catch (Exception e) {
            Log.e(TAG, "get: get response data from futureTask failed.", e);
        }
        Log.d(TAG, "get: " + responseJsonData);
        return responseJsonData;
    }

    public static String post(String url, Object params) {
        FutureTask<String> responseTask = new FutureTask<>(() -> {
            String responseJsonData = null;
            Gson gson = new Gson();
            RequestBody responseBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), gson.toJson(params));
            try {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .post(responseBody)
                        .url(url)
                        .build();
                Call call = client.newCall(request);
                Response response = call.execute();
                responseJsonData = response.body().string();
            } catch (Exception e) {
                Log.e(TAG, "get: " + e);
            }
            return responseJsonData;
        });

        Thread responseThread = new Thread(responseTask);
        responseThread.start();
        String responseJsonData = null;
        try {
            responseJsonData = responseTask.get();
        } catch (Exception e) {
            Log.e(TAG, "get: get response data from futureTask failed.", e);
        }
        Log.d(TAG, "get: " + responseJsonData);
        return responseJsonData;
    }

    private static Map<String, String> convertObjToMap(Object obj) {
        Gson gson = new Gson();
        String json = gson.toJson(obj);
        return gson.fromJson(json, new TypeToken<Map<String, String>>() {
        }.getType());
    }
}
